package com.entor.hqj.main;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.entor.hqj.login.LoginActivity;
import com.entor.hqj.login.R;
import com.entor.hqj.model.User;
import com.entor.hqj.util.HttpPostUtils;
import com.entor.hqj.util.MoreTextView;
import com.entor.hqj.util.TitleBuilder;

import java.net.URLDecoder;
import java.net.URLEncoder;

public class EditInfoActivity extends Activity implements View.OnClickListener{

    private MoreTextView et_login;
    private EditText et_title;
    private EditText et_content;
    private RadioButton sex_boy;
    private RadioButton sex_gril;
    private EditText et_age;
    private Long id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_info);

        et_login = (MoreTextView)findViewById(R.id.et_login);
        et_title = (EditText)findViewById(R.id.et_title);
        et_content = (EditText)findViewById(R.id.et_content);
        sex_boy = (RadioButton)findViewById(R.id.sex_boy);
        sex_gril = (RadioButton)findViewById(R.id.sex_gril);
        et_age = (EditText)findViewById(R.id.et_age);

        initTitle();
        initData();
    }

    public void initTitle(){
        new TitleBuilder(this).setLeftText("取消")
                .setRightText("修改")
                .setLeftImgOrTextListener(this)
                .setRightImgOrTextListener(this);
    }

    public void initData(){
        User u = LoginActivity.userInfo;
        if(u!=null){
            id = u.getId();
            et_login.setText(u.getLoginname());
            et_title.setText(u.getName());
            et_content.setText(u.getContent());
            int sex = u.getSex();
            if(sex==0)
                sex_boy.setChecked(true);
            else
                sex_gril.setChecked(true);
            et_age.setText(u.getAge()+"");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.title_left_textview:
                finish();
                break;
            case R.id.title_right_textview:
                update();
                break;
        }
    }

    public void update(){
        try {
            String name = et_title.getText().toString().trim();
            String content = et_content.getText().toString().trim();
            String age = et_age.getText().toString().trim();
            int sex = sex_boy.isChecked() ? 0 : 1;

            final String data = "id=" + id + "&name="
                    + name
                    + "&content="
                    + URLDecoder.decode(content)
                    + "&age=" + age + "&sex=" + sex;
            new Thread() {
                @Override
                public void run() {
                    String response =
                            HttpPostUtils.doPostRequest("user/editByMobile", data);
                    Message msg = Message.obtain();
                    msg.obj = response;
                    handler.sendMessage(msg);
                }
            }.start();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message){
            String result = (String)message.obj;
            JSONObject resultJson =
                    JSON.parseObject(result);
            if(resultJson!=null) {
                String msg = resultJson.getString("msg");
                boolean sus = resultJson.getBoolean("success");
                User user = resultJson.getObject("obj",User.class);
                if(sus&&user!=null){
                    LoginActivity.userInfo = user;
                }
                Toast.makeText(EditInfoActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        }
    };
}
